//
//  STAreasTitleView.m
//  赛拓
//
//  Created by 王磊 on 8/16/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STAreasTitleView.h"
#import "STAreasTitleCellView.h"
#import "STAreasHeaderView.h"

@interface STAreasTitleView()<STBaseViewDelegate>

@property (nonatomic ,strong) NSString *totalAreaName;

@property (nonatomic ,strong) STAreasHeaderView *headerView;
@end

#define Item_Width ST_SCREEN_WIDTH / 4
#define Item_MidSpace 5

static NSString *const headerIdentifer = @"headerIdentifer";
static NSString *const cellIdentifer = @"cellIdentifer";
@implementation STAreasTitleView

- (void)commitInit {
    [super commitInit];
    
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    [self registerClass:[STAreasTitleCellView class] forCellWithReuseIdentifier:cellIdentifer];
    
    [self.layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    [self registerClass:[STAreasHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifer];
    
    self.layout.sectionInset = UIEdgeInsetsMake(0, Item_MidSpace, 0, 0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    STAreasTitleCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifer forIndexPath:indexPath];
    
    [cell updateData:self.dataArray[indexPath.row]];
    
    cell.mDelegate = self;
    
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        STAreasHeaderView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifer forIndexPath:indexPath];
        
        [view updateData:self.totalAreaName];
        
        view.mDelegate = self;
        
        self.headerView = view;
        
        return view;
    }
    return nil;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(Item_Width, self.k_height);
    
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    
    
    switch (itemType) {
        case STItemClickTypeTotalArea:
        {
            if (self.contentOffset.x != 0) {
                [self setContentOffset:CGPointMake(0, 0) animated:true ];
            }
        }
            break;
        default:
            break;
    }
    [self.mDelegate onItemClick:itemType withData:data forIndexPath:indexPath];
}

#pragma mark --- collection view delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        
        [collectionView selectItemAtIndexPath:indexPath animated:true scrollPosition:UICollectionViewScrollPositionNone];
        
        CGFloat x = (indexPath.row + 1 + 1) * (Item_Width + Item_MidSpace);
        
        CGFloat temx = Item_Width + Item_MidSpace;
        
        if (indexPath.row + 1 == self.dataArray.count) {
            
            [self setContentOffset:CGPointMake( x - ST_SCREEN_WIDTH + self.k_height, 0) animated:true];
        } else if (indexPath.row == 0){
            
            [self setContentOffset:CGPointMake(0, 0) animated:true];
            
        } else {
            
            [self setContentOffset:CGPointMake(temx + x - ST_SCREEN_WIDTH + self.k_height, 0) animated:true];
        }
        
        [self.mDelegate onItemClick:STItemClickTypeArea withData:self.dataArray[indexPath.row] forIndexPath:indexPath];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return  CGSizeMake(Item_Width, self.k_height);
}
- (void)updateData:(id)data {
    
    if (self.dataArray.count) {
        [self.dataArray removeAllObjects];
    }
    
    [self.dataArray addObjectsFromArray:data];
    
    [self reloadData];
}
- (void)updateHeader:(id)data {
    
    self.totalAreaName = data;
    
}
@end
